﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data.SqlClient;
using System.Data;

namespace TAMSUP
{
    /// <summary>
    /// Interaction logic for PembayaranForm.xaml
    /// </summary>
    public partial class PembayaranForm : Window
    {
        private string ID = "";
        private bool isUbah = false;
        private bool isSelected = false;
        private DataSet ubah = new DataSet();
        public PembayaranForm()
        {
            InitializeComponent();
        }

        public PembayaranForm(string ID)
        {
            this.ID = ID;
            isUbah = true;
            InitializeComponent();
            label1.Content = "Ubah Pembayaran";
            addButton.Content = "Edit";
            cancelButton.Content = "Cancel";
        }

        public PembayaranForm(DataRowView row)
        {
            InitializeComponent();
            sewaBox.Items.Add(row.Row.ItemArray.ElementAt(0).ToString());
            sewaBox.SelectedIndex = 0;
            isSelected = true;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {

                homeWindow.Con.Open();

                tanggalDate.SelectedDate = DateTime.Now;
                string _Date1 = tanggalDate.SelectedDate.ToString();

                SqlDataAdapter da = new SqlDataAdapter("select * from penyewaan as a inner join menyewa as b on a.ID_Penyewaan = b.ID_Penyewaan", homeWindow.Con);
                DataSet ds = new DataSet();
                da.Fill(ds, "Daftar Penyewaan");

                if (!isSelected)
                {
                    int count = ds.Tables["Daftar Penyewaan"].Rows.Count + 1;

                    for (int i = 0; i < count - 1; i++)
                    {
                        sewaBox.Items.Add(ds.Tables["Daftar Penyewaan"].Rows[i].ItemArray.ElementAt(0).ToString());
                    }
                }

                if (isUbah)
                {
                    da = new SqlDataAdapter("select * from pembayaran as a inner join penyewaan as b on a.ID_Penyewaan = b.ID_Penyewaan where a.ID_Pembayaran = '" + ID + "';", homeWindow.Con);
                    ubah = new DataSet();
                    da.Fill(ubah, "Daftar Pembayaran");
                    sewaBox.SelectedItem = ubah.Tables["Daftar Pembayaran"].Rows[0].ItemArray.ElementAt(10).ToString();
                    urutanText.Text = ubah.Tables["Daftar Pembayaran"].Rows[0].ItemArray.ElementAt(1).ToString();
                    jumlahText.Text = ubah.Tables["Daftar Pembayaran"].Rows[0].ItemArray.ElementAt(3).ToString();
                    tanggalDate.Text = ubah.Tables["Daftar Pembayaran"].Rows[0].ItemArray.ElementAt(2).ToString();
                    invoiceText.Text = ubah.Tables["Daftar Pembayaran"].Rows[0].ItemArray.ElementAt(8).ToString();
                    fakturText.Text = ubah.Tables["Daftar Pembayaran"].Rows[0].ItemArray.ElementAt(9).ToString();
                }
                homeWindow.Con.Close();
            }
            catch (SqlException sqle)
            {
                MessageBox.Show("Ada kesalahan pada koneksi ke server, silakan periksa jaringan.\n\n SQL error code : " + sqle.State, "Error : Connection Not Established");
            }
        }

        private void addButton_Click(object sender, RoutedEventArgs e)
        {
           
            if (errorChecking())
            {
                try {
                    homeWindow.Con.Open();
                    if (!isUbah)
                    {
                        string _Date1 = tanggalDate.SelectedDate.Value.ToString("M/d/yyyy");

                        string _Insert = @"insert into pembayaran (ID_Pembayaran, Pembayaran_ke, Tanggal_Pembayaran, Jumlah_Pembayaran, ID_Penyewaan, No_Invoice, No_Faktur)
                                  Values('B" + "','" + urutanText.Text + "','" + _Date1 + "','" +
                                                    jumlahText.Text + "','" + sewaBox.SelectedItem.ToString() + "','" + invoiceText.Text + "','" + fakturText.Text + "')";

                        SqlCommand _cmd = new SqlCommand(_Insert, homeWindow.Con);

                        // Execute the command
                        _cmd.ExecuteReader();
                        MessageBox.Show("Berhasil memasukkan informasi pembayaran baru", "New Payment");
                        homeWindow.Con.Close();                    
                        homeWindow.top.refresh();
                        restart();
                    }
                    else
                    {
                        string _Date1 = tanggalDate.SelectedDate.Value.ToString("M/d/yyyy");

                        string _Insert = @"update Pembayaran
                                        set Pembayaran_Ke = '" + urutanText.Text + "', Tanggal_Pembayaran = '" + _Date1 + "',  Jumlah_Pembayaran = '" + jumlahText.Text +
                                                            "',  ID_Penyewaan = '" + sewaBox.SelectedItem.ToString() + "', No_Invoice = '" + invoiceText.Text + "' , No_Faktur = '" + fakturText.Text + "'" +
                                                            "where ID_Pembayaran = '" + ID + "';";

                        SqlCommand _cmd = new SqlCommand(_Insert, homeWindow.Con);
                        _cmd.ExecuteNonQuery();
                        MessageBox.Show("Berhasil mengubah informasi pembayaran", "Update Payment");
                        homeWindow.Con.Close();                    
                        homeWindow.top.refresh();
                        homeWindow.top.Focus();
                        this.Close();
                    }
                }
                catch (SqlException sqle)
                {
                    MessageBox.Show("Ada kesalahan pada koneksi ke server, silakan periksa jaringan.\n\n SQL error code : " + sqle.State, "Error : Connection Not Established");
                }
            }
        }

        private void restart()
        {
            Brush validColor = new SolidColorBrush(Color.FromRgb(200, 200, 200));
            sewaBox.SelectedIndex = -1;
            sewaBox.Background = validColor;
            urutanText.Text = "";
            urutanText.Background = validColor;
            jumlahText.Text = "";
            jumlahText.Background = validColor;
            tanggalDate.SelectedDate = DateTime.Now;
            tanggalDate.Background = validColor;
        }

        private Boolean errorChecking()
        {
            Boolean valid = true;
            Brush invalidColor = new SolidColorBrush(Color.FromRgb(255, 225, 0));
            Brush validColor = new SolidColorBrush(Color.FromRgb(200, 200, 200));
            int temp;
            if (sewaBox.SelectedIndex == -1)
            {
                sewaBox.Background = invalidColor;
                if (valid) valid = false;
            }
            else
            {
                sewaBox.Background = validColor;
            }
            if ((urutanText.Text == "") || (Int32.Parse(urutanText.Text) > 99) || !Int32.TryParse(urutanText.Text.ToString(), out temp))
            {
                urutanText.Background = invalidColor;
                if (valid) valid = false;
            }
            else
            {
                urutanText.Background = validColor;
            }
            if (jumlahText.Text == "" || !Int32.TryParse(jumlahText.Text.ToString(), out temp))
            {
                jumlahText.Background = invalidColor;
                if (valid) valid = false;
            }
            else
            {
                jumlahText.Background = validColor;
            }
            if (!valid)
            {
                MessageBox.Show("Ada kesalahan pengisian informasi penyewaan", "Invalid Information");
            }
            return valid;
        }

        private void cancelButton_Click(object sender, RoutedEventArgs e)
        {
            if (isUbah)
            {
                homeWindow.top.refresh();
                homeWindow.top.Focus();
                this.Close();
            }
            else
            {
                restart();
            }
        }

        private void formWindow_Closed(object sender, EventArgs e)
        {
            homeWindow.top.refresh();
            homeWindow.top.Focus();
        }

        private string addNol(int x)
        {
            string temp = "";
            string angka = x.ToString();

            if (angka.Count() == 4)
            {
                temp = "0";
            }
            else if (angka.Count() == 3)
            {
                temp = "00";
            }
            else if (angka.Count() == 2)
            {
                temp = "000";
            }
            else if (angka.Count() == 1)
            {
                temp = "0000";
            }
            Console.WriteLine(temp);
            return temp;
        }
    }
}
